//
//  main.m
//  PlugSuit
//
//  Created by ∞ on 28/09/07.
//  Copyright __MyCompanyName__ 2007. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <string.h>
#import <sys/types.h>
#import <unistd.h>
#import <stdio.h>

int main(int argc, char *argv[]) {
	if (argc == 2 && strcmp(argv[1], "--L0Authorize") == 0) {
		if (geteuid() == 0) {
			NSAutoreleasePool* pool = [NSAutoreleasePool new];
			
			NSString* selfPath = [[NSBundle mainBundle] executablePath];
			const char* selfPathC = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:selfPath];
			
			int i;
			
			if ((i = chown(selfPathC, -1, 9 /* procmod */)) != 0)
				fprintf(stderr, "[from PlugSuit]: Can't give ownership to the procmod group. Sorry.\n");
			
			if (i == 0 && (i = chmod(selfPathC, 02755)) != 0) // setgid bit set
				fprintf(stderr, "[from PlugSuit]: Can't set the setgid bit. Sorry.\n");			
			
			[pool release];
			return i? 1 : 0;
		} else {
			fprintf(stderr, "[from PlugSuit]: To authorize, you must run this as root or use the preference pane's UI. Sorry.\n");
			return 1;
		}
	}
    
	return NSApplicationMain(argc,  (const char **) argv);
}
